<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\controller\adminapi\diy;

use think\App;
use hiduoke\services\CloudServer;
use hiduoke\services\cloud\DiyCloud;
use hiduoke\controller\BaseController;
use hiduoke\services\FormBuilder as Form;

/**
 * Notes: 云端DIY管理
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 * Created on:  2025/8/6 03:29
 */
class CloudDiy extends BaseController
{
    /**
     * @var DiyCloud
     */
    private DiyCloud $Diy;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->Diy = new DiyCloud();
    }

    //获取DIY列表
    public function getList($page_name, $page = 1, $search = [], $size = 12)
    {
        $res = (new CloudServer())->getSite();
        if ($search['look'] == 'me') {
            $search['site_id'] = $res['data']['id'] ?? 0;
        } elseif ($search['look'] == 'other') {
            $search['site_id <>'] = $res['data']['id'] ?? 0;
        }
        unset($search['look']);
        // TODO 这里前端没有报错
        return $this->Diy->getList($page_name, $page, $search, $size);
    }


    //下载DIY数据
    public function down($id)
    {
        return $this->Diy->down($id);
    }

    /**
     * 获取提交diy的表单
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function getDiyForm()
    {
        $rule = array();
        $id = input('id', 0);
        $page_name = input('page_name', '');
        $diy_data = input('diy_data', '');
        if ($id) {
            $res = $this->Diy->getDiy($id);
            if (!$res['code'] == 0) {
                return $this->error('当前模板不存在');
            } else {
                $row = $res['data'];
            }
        } else {
            $row = [];
        }
        $form = Form::createForm("/adminapi/cloud_diy/saveCloudDiy")->setMethod("POST");
        //新增表单
        $rule[] = Form::hidden('id', $id);
        $rule[] = Form::dkInput('name', '模板名称', $row['name'] ?? '')->effect(['help'=>'设置模板标题，如果想自己用不审核可以备注一下'])->required();
        $rule[] = Form::dkImage('cover_image', '封面', $row['cover_image'] ?? '')->effect(['help' => '推荐使用180*240的图'])->required();
        $rule[] = Form::input('desc', '模板描述', $row['desc'] ?? '');
        $rule[] = Form::hidden('diy_data', $diy_data);
        $rule[] = Form::hidden('page_name', $page_name);
        $rule[] = Form::input('size', '当前模板大小', $this->Diy->getDiyDataSize($diy_data))->disabled(true)
            ->effect(['help' => '当前云端数据的大小，数据不能超过500kb,图片资源请使用对象存储']);
        if ($id) {
            $rule[] = Form::switches('update_diy_data', '更新数据', '0')
                ->inactiveText('否')
                ->activeText('是')
                ->effect(['help' => '选择【是】将使用当前页面的DIY数据覆盖到云端']);
        }
        $title = !$id ? "创建新模板" : "编辑模板";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存DIY数据
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function saveCloudDiy()
    {
        $formData = getFormData();
        if (!$this->Diy->checkDiyData($formData['diy_data'])) {
            return $this->error("云端数据不得大于500kb");
        }
        if ($formData['id'] > 0 && $formData['update_diy_data'] == 0) {
            unset($formData['diy_data']);
        }
        unset($formData['size']);
        $res = $this->Diy->add($formData);
        if ($res['code'] == 0) {
            return $this->success($res['msg'], $res['data'], $formData);
        } else {
            return $this->error($res['msg'], $res['data']);
        }
    }

    /**
     * 获取站点等级 TODO 这里后续优化，独立分配权限
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function getSiteMsg()
    {
        $res = (new CloudServer())->getSite();
        if ($res['code'] == 0) {
            $data = [
                'level'   => $res['data']['level'],
                'site_id' => $res['data']['id']
            ];
        } else {
            $data = [];
        }
        return $this->success('', $data);
    }

    /**
     * 获取审核表单
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function getCheckForm()
    {
        $rule = array();
        $id = input('id', 0);
        if (!$id) {
            return $this->error('未找到当前模板');
        }
        $state = input('state/d', 0);
        $form = Form::createForm("/adminapi/cloud_diy/checkDiy")->setMethod("POST");
        $rule[] = Form::hidden('id', $id);
        //新增表单
        $rule[] = Form::radio('state', '当前模板', $state)->options([
            ['label' => '拒绝', 'value' => -1],
            ['label' => '审核中', 'value' => 0],
            ['label' => '通过', 'value' => 1],
            ['label' => '推荐', 'value' => 2],
        ])->appendControl(-1, [
            Form::input('remark', '拒绝原因', '')
        ]);
        $title = "模板审核";
        $form->setTitle($title)->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }


    public function checkDiy()
    {
        $formData = getFormData();
        $res = $this->Diy->check($formData);
        return $res;
    }

    public function delDiy()
    {
        $id = input('id', 0);
        return $this->Diy->del($id);
    }
}
